package cn.mulanbay.admin.api.persistent.service;

import cn.mulanbay.common.bean.PageSearch;
import cn.mulanbay.common.exception.PersistentException;
import cn.mulanbay.persistent.entity.DictGroup;
import cn.mulanbay.persistent.mapper.DictGroupMapper;
import cn.mulanbay.persistent.mapper.DictItemMapper;
import cn.mulanbay.persistent.query.QueryBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fenghong
 * @since 2022-02-06
 */
@Service
public class DictGroupService extends ServiceImpl<DictGroupMapper, DictGroup>  {

    @Autowired
    DictGroupMapper dictGroupMapper;

    @Autowired
    DictItemMapper dictItemMapper;

    /**
     * 获取列表
     *
     * @param us
     * @return
     */
    public Page<DictGroup> getDataList(PageSearch us){
        Page<DictGroup> pp = new Page<>(us.getPage(),us.getPageSize());
        QueryWrapper qw = QueryBuilder.build(us);
        pp.addOrder(OrderItem.asc("order_index"));
        if(us.getPage()<0){
            List<DictGroup> list = baseMapper.selectList(qw);
            pp.setRecords(list);
        }else{
            pp = baseMapper.selectPage(pp,qw);
        }
        return pp;
    }

    @Transactional(rollbackFor = PersistentException.class)
    public void deleteDictGroup(Long id){
        dictItemMapper.deleteItems(id);
        dictGroupMapper.deleteById(id);
    }
}
